#include "CPluginDlg.h"
#include <qlabel.h>
#include <qtreewidget.h>
#include <QTreeWidgetItem>
#include <qpushbutton.h>
#include <qicon.h>
#include <qheaderview.h>
#include <qgridlayout.h>
#include <qdir.h>
#include <qpluginloader.h>

#include "ui_CPluginDlg.h"

namespace Ui {
	class CPluginDlgClass;
}
Ui::CPluginDlgClass ui;

#pragma execution_character_set("utf-8")
class CPluginDlgPrivate
{
public:
	explicit CPluginDlgPrivate(CPluginDlg* parent);
	~CPluginDlgPrivate();
private:
	CPluginDlg* const q_ptr;
	QLabel* m_label;
	QTreeWidget* m_treewidget;
	QPushButton* m_ok_btn;
	QIcon m_interface_icon;
	QIcon m_feature_icon;
	QString m_plug_path;
	QStringList m_filenames;
	Q_DECLARE_PUBLIC(CPluginDlg)
};

CPluginDlg::CPluginDlg(QWidget *parent)
	: QDialog(parent), d_ptr{new CPluginDlgPrivate(this)}
{
	ui.setupUi(this);
	Q_ASSERT(d_ptr);
}

CPluginDlg::CPluginDlg(QStringView plug_path, const QStringList& filenames, QWidget* parent)
	:CPluginDlg(parent)
{
	OnInitDialog();
}

CPluginDlg::~CPluginDlg()
{
	if (d_ptr)
		delete d_ptr;
}

void CPluginDlg::OnInitDialog()
{
	this->setWindowTitle(tr("Plugin Dialog"));
	this->setWindowFlags(this->windowFlags() &(Qt::WindowContextHelpButtonHint));
	if (d_ptr)
	{
		d_ptr->m_label = new QLabel(this);
		d_ptr->m_treewidget = new QTreeWidget(this);
		d_ptr->m_ok_btn = new QPushButton(tr("OK"));
		d_ptr->m_treewidget->setAlternatingRowColors(false);
		d_ptr->m_treewidget->setSelectionMode(QAbstractItemView::NoSelection);
		d_ptr->m_treewidget->setColumnCount(1);
		d_ptr->m_treewidget->header()->hide();

		d_ptr->m_ok_btn->setDefault(true);
		connect(d_ptr->m_ok_btn, &QPushButton::clicked, this, &QDialog::close);

		auto _main_layout = new QGridLayout(this);
		_main_layout->setColumnStretch(0, 1);
		_main_layout->setColumnStretch(2, 1);
		_main_layout->addWidget(d_ptr->m_label,0,0,1,3);
		_main_layout->addWidget(d_ptr->m_treewidget,1,0,1,3);
		_main_layout->addWidget(d_ptr->m_ok_btn,2,1);
		this->setLayout(_main_layout);

		d_ptr->m_interface_icon.addPixmap(this->style()->standardPixmap(QStyle::SP_DirOpenIcon),QIcon::Normal,QIcon::On);
		d_ptr->m_interface_icon.addPixmap(this->style()->standardPixmap(QStyle::SP_DirClosedIcon),QIcon::Normal,QIcon::Off);
		FindPlguins(d_ptr->m_plug_path, d_ptr->m_filenames);
	}
}

void CPluginDlg::FindPlguins(QStringView plug_path, const QStringList& filenames)
{
	if (d_ptr)
	{
		d_ptr->m_label->setText(tr("Plugin found the follwing plugin\n""(looked in %1)").arg(QDir::toNativeSeparators(plug_path.toString())));
		const QDir _dir(plug_path.toString());
		const auto _static_instances = QPluginLoader::staticInstances();
		for (const auto i : _static_instances)
			PopulateTreeWidget(i, tr("%1 (static plugin)").arg(i->metaObject()->className()));
		for (const auto& j : filenames)
		{
			QPluginLoader _loader(_dir.absoluteFilePath(j));
			auto _plugin = _loader.instance();
			if (_plugin)
				PopulateTreeWidget(_plugin, j);
		}
	}
}

void CPluginDlg::PopulateTreeWidget(QObject* plugin, QStringView text)
{
}

void CPluginDlg::AddItems(QTreeWidgetItem* plug_item, const char* interface_name, const QStringList& features)
{
}

CPluginDlgPrivate::CPluginDlgPrivate(CPluginDlg* parent)
	:q_ptr{ parent }, m_label{}, m_treewidget{}, m_ok_btn{}, m_plug_path{}, m_filenames{}
{
}

CPluginDlgPrivate::~CPluginDlgPrivate()
{
}
